
#include "byte_process.h"
#include <cstdint>

uint16_t HToBe16(uint8_t * data, int length)
{
    uint16_t value = 0;
    uint8_t * unit = (uint8_t*)&value;

    unit[0] = data[1];
    unit[1] = data[0];

    return value;
}

uint32_t HToBe32(uint8_t * data, int length)
{
    uint32_t value = 0;
    uint8_t * unit = (uint8_t*)&value;

    unit[0] = data[3];
    unit[1] = data[2];
    unit[2] = data[1];
    unit[3] = data[0];

    return value;
}

uint64_t HToBe64(uint8_t * data, int length)
{
    uint64_t value = 0;

    uint8_t * unit = (uint8_t*)&value;

    unit[0] = data[7];
    unit[1] = data[6];
    unit[2] = data[5];
    unit[3] = data[4];
    unit[4] = data[3];
    unit[5] = data[2];
    unit[6] = data[1];
    unit[7] = data[0];

    return value;
}

uint64_t HToBeN(uint8_t * data, int length, int bytes)
{
    uint64_t value = 0;
    uint8_t * unit = (uint8_t*)&value;

    for (int index = 0; index < bytes; index++)
    {
        unit[bytes - index - 1] = data[index];
    }
    return value;
}

uint16_t BeToH16(const uint8_t * data, int length)
{
    uint16_t value = 0;
    uint8_t * unit = (uint8_t*)&value;

    unit[0] = data[1];
    unit[1] = data[0];

    return value;
}

uint32_t BeToH32(const uint8_t * data, int length)
{
    uint32_t value = 0;
    uint8_t  * unit = (uint8_t*)&value;

    unit[0] = data[3];
    unit[1] = data[2];
    unit[2] = data[1];
    unit[3] = data[0];

    return value;
}

uint64_t BeToH64(const uint8_t * data, int length)
{
    uint64_t value = 0;

    uint8_t * unit = (uint8_t*)&value;

    unit[0] = data[7];
    unit[1] = data[6];
    unit[2] = data[5];
    unit[3] = data[4];
    unit[4] = data[3];
    unit[5] = data[2];
    unit[6] = data[1];
    unit[7] = data[0];

    return value;
}

uint64_t BeToHn(uint8_t * data, int length, int bytes)
{
    uint64_t value = 0;

    uint8_t * unit = (uint8_t*)&value;

    for (int index = 0; index < bytes; index++)
    {
        unit[bytes - index - 1] = data[index];
    }
    return value;
}
